package com.hcloud.baselib.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hcloud.baselib.common.SingleLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

open class BaseViewModel : ViewModel() {
    val loadingLiveData = SingleLiveData<Boolean>()
    val errorLiveData = SingleLiveData<Throwable>()

    fun launch(block: suspend () -> Unit) {

        loadingLiveData.postValue(true)

        viewModelScope.launch(Dispatchers.IO) {
            try {
                block()
            } catch (e: Exception) {
                error(e)
            }
        }
    }
}